///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Scripts.h>
#include <lua.hpp>
//-----------------------------------------------------------------------------

Script*			script	= NULL;
Object*			ScriptObject	= NULL;

//-----------------------------------------------------------------------------

void	ScriptsInput()
{
	if( InputManager::GetInstance()->IsKeyDown(' ') )
	{
		if( ScriptObject )
		{
			script->RunFunction( "move", ScriptObject );
		}
	}

	if( InputManager::GetInstance()->IsKeyDown(',') )
	{
		if( ScriptObject )
		{
			ScriptObject->rotateAroundY( 1.0f );
		}
	}
	if( InputManager::GetInstance()->IsKeyDown('.') )
	{
		if( ScriptObject ) 
		{
			ScriptObject->rotateAroundY( -1.0f );
		}
	}
}

//-----------------------------------------------------------------------------

int ObjectMove(lua_State* L)
{
	int n = lua_gettop(L);    /* number of arguments */
	if( n != 2 
		|| !lua_isnumber(L, 1)
		|| !lua_isnumber(L, 2))
	{
		lua_pushstring(L, "incorrect argument");
		lua_error(L);
		lua_pushboolean(L, false);
		return 1;
	}
	
	lua_Number obj = lua_tonumber(L, 1);
	lua_Number dist = lua_tonumber(L, 2);
	int adr = obj;
	((Object*)adr)->moveAlongForward( (f32)dist );

	lua_pushboolean(L, true);
	return 1;
}

//-----------------------------------------------------------------------------

int sayHello(lua_State* L)
{
	int n = lua_gettop(L);    /* number of arguments */
	if( n > 0 )
	{
		lua_pushstring(L, "incorrect argument");
		lua_error(L);
		return 0;
	}
	lua_pushstring(L, "Hello");
	return 1;
}

//-----------------------------------------------------------------------------

void	ScriptsLoad(SceneManager* scene)
{
	script = ScriptManager::GetInstance()->GetScript( "script.lua" );
	ASSERT( script );
	script->RunScript();
	script->RunFunction( "run" );
	script->RegisterFunction( "cfunc", sayHello );
	script->RunFunction( "run1" );

	script->RegisterFunction( "ObjectMove", ObjectMove );

	ScriptObject = scene->GetRootNode()->GetChildren().size() ? scene->GetRootNode()->GetChildren()[1] : NULL;
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
